$(function(){
	//店铺商品详情和评价切换
	$('.store .fourth .right .top span.detail').click(function(){
		$(this).addClass('selected').next().removeClass('selected');
		$(this).css('borderLeft','0');
		$(this).next().css('borderLeft','0');
		$('.store .fourth .right .details').css('display','block');
		$('.store .fourth .right .evaluation').css('display','none');
	});
	
	$('.store .fourth .right .top span.eval').click(function(){
		$(this).addClass('selected').prev().removeClass('selected');
		$(this).css('borderLeft','1px solid #ccc');
		$('.store .fourth .right .details').css('display','none');
		$('.store .fourth .right .evaluation').css('display','block');
	});
	
	$('.store .third .left div > span > img').hover(function(){
		var $src = $(this).attr('src');
		$(this).parents('.left').children('img').attr('src',$src);
	})
	
	$('.store .third div .c .size span').click(function(){
		$(this).addClass('selected').siblings().removeClass('selected');
	});
	$('.store .third div .c .color span').click(function(){
		$(this).addClass('selected').siblings().removeClass('selected');
	});
	
	//点击收藏
	$('.store .collect img').click(function(){
		if($(this).attr('src')== rootPath + '/images/collect2.png'){
			$(this).attr('src', rootPath + '/images/sch.png');//收藏
			addOrCacelFavorites("1"); //商品是否已被收藏（0：否，1：是）
		}else{
			$(this).attr('src', rootPath + '/images/collect2.png'); //取消收藏
			addOrCacelFavorites("0"); //商品是否已被收藏（0：否，1：是）
		};
		//商品收藏人气
		goodsCollectionsentiment();
	});
	//数量 +
	$('.store .add').click(function(){
		var $number = $(this).siblings('input').val();
		var $inventory = $(this).parent().find('.inventory span').text();
		if(parseInt($number)< parseInt($inventory)){
			$number++;
		}else{
			$(this).css('cursor','not-allowed');
		}
		$(this).siblings('input').val($number);
	});
	
	//数量 —
	$('.store .minus').click(function(){
		var $number = $(this).siblings('input').val();
		var $inventory = $(this).parent().find('.inventory span').text();
		if($number>1){
			$number--;
		}else{
			$(this).css('cursor','not-allowed');
		}
		$(this).siblings('input').val($number);
	});
	
	//商品收藏人气
	goodsCollectionsentiment();
	
	//商品促销活动
	goodsProList();
	
	//获取商品库存
	updateGoodsStock();
	
	//点击规格更新库存值
	$('.store .third div .c .color > span').click(function(){
		var $color =$(this) .text();
		var $size = $('.store .third div .c .size .selected');
		$size  = $size && $size.length >0 ? $size.text():"";	    
		
		var $colori = $('.store .third div .c .color  > i');
		$colori = $colori && $colori.length >0 ? $colori.text():"";	

		var $sizei = $('.store .third div .c .size  > i');
		$sizei = $sizei && $sizei.length >0 ? $sizei.text():"";

		var  skuInfo = $colori + ":" + $color +","+ $sizei+":" + $size;
		
		var stock = getGoodsStock(skuInfo);
		var $input = $('.store .third div .c .number input');
		var $mount = parseInt($input.val());
		if($mount > stock){//判断输入的商品数量是否超库存
			layer.msg("商品数量超库存了!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
	    	$input.val(stock);
			if(stock <= 0){
			   $(this).addClass("nine");
			   $('.store .third div .c .size .selected').addClass("nine");
			   $input.val(1);
			}else{
				$(this).removeClass("nine");
				 $('.store .third div .c .size .selected').removeClass("nine");
			}
	    }else{
	    	 $('.store .third div .c .size .selected').removeClass("nine");
			$(this).removeClass("nine");
	    }
	    $(".store .third div .c div .inventory > span").text(stock); //更新库存值
	});
	$('.store .third div .c .size > span').click(function(){
		var $size =$(this) .text();
		var $color = $('.store .third div .c .color .selected');
		$color  = $color && $color.length >0 ? $color.text():"";	    
		
		var $sizei = $('.store .third div .c .size  > i');	
		$sizei = $sizei && $sizei.length >0 ? $sizei.text():"";

		var $colori = $('.store .third div .c .color  > i');
		$colori = $colori && $colori.length >0 ? $colori.text():"";		

		var  skuInfo = $colori + ":" + $color +","+ $sizei+":" + $size;
		
		var stock = getGoodsStock(skuInfo);
		var $input = $('.store .third div .c .number input');
		var $mount = parseInt($input.val());
		if($mount > stock){ //判断输入的商品数量是否超库存
			layer.msg("商品数量超库存了!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
			$input.val(stock);
			if(stock <= 0){
			   $(this).addClass("nine");
			   $('.store .third div .c .color .selected').addClass("nine");
			   $input.val(1);
			}else{
				$(this).removeClass("nine");
				 $('.store .third div .c .color .selected').removeClass("nine");
			}
	    }else{
			$(this).removeClass("nine");
			 $('.store .third div .c .color .selected').removeClass("nine");
	    }
	    $(".store .third div .c div .inventory > span").text(stock); //更新库存值
	});
	
	//监听商品数量不能超过库存
	$('.store .third div .c .number input').change(function(){
	    var $stock =  $(".store .third div .c div .inventory > span").text();
	    $stock = parseInt($stock);
	    if($(this).val() > $stock){
			layer.msg("商品数量超库存了!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
	    	$(this).val($stock);
	    	
	    }
	    if($(this).val() < 1 || !$(this).val() || isNaN($(this).val()) || $(this).val()=="" ){
    		$(this).val(1);
    	}
	});
	
});
//根据选中的规格参数计算商品的库存值
function updateGoodsStock(){
	var goodsskuvalueid = $('.store .third div .c .color > span').attr("goodsskuvalueid");
	var $color = $('.store .third div .c .color .selected');
	var $size = $('.store .third div .c .size .selected');
	
	$color = $color && $color.length >0 ? $color.text():"";
	$size  = $size && $size.length >0 ? $size.text():"";
    
	
	var $colori = $('.store .third div .c .color  > i');
	$colori = $colori && $colori.length >0 ? $colori.text():"";
	

	var $sizei = $('.store .third div .c .size  > i');
	$sizei = $sizei && $sizei.length >0 ? $sizei.text():"";

	var skuInfo = $color && $size ? ($colori + ":" + $color +","+ $sizei+":" + $size):"";
	var stock = getGoodsStock(skuInfo);
	var $input = $('.store .third div .c .number input');
	var $mount = parseInt($input.val());
	if($mount > stock){
		layer.msg("商品数量超库存了",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
    	$input.val(stock);
    }
    $(".store .third div .c div .inventory > span").text(stock);
	
}
//获取商品库存
function getGoodsStock(skuInfo){
	var url = rootPath + "/cart/getGoodsStockBySkuValues";
	var data={};
	data["productId"] = $("#goodsIdd").val();
	data['goods_sku_info']=skuInfo?skuInfo:"";
	var url = rootPath + '/cart/getGoodsStockBySkuValues';
	var result = CommnUtil.ajax(url, data, "json");	
	if(result.resultSuccess){
		//alert("商品库存：" + result.stock)
		return result.stock;
	}else{
		layer.msg("商品库存读取失败",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});	
	}
	return 0;
}
/**
 * @title 查询该商品收藏人气
 */
function goodsCollectionsentiment(){
	var url = rootPath + "/goods/goodsCollectionSentiment";
	var result = CommnUtil.ajax(url,{"goodsId":$("#goodsIdd").val()},"json");
	if (result.resultSuccess) {
		$(".store .third .left div p .collect > i").text(result.count);
	}else {	
		layer.msg("查询商品收藏人气失败!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
	}
}

/**
 * @title 商品促销活动
 */
function goodsProList(){
	var url = rootPath + "/goods/goodsProList";
	var result = CommnUtil.ajax(url,{"goodsId":$("#goodsIdd").val()},"json");
	if(CommnUtil.notNull(result)){
		if (result.resultSuccess) {
			var data = result.data;
			if(CommnUtil.notNull(data)&&data!=""){
				$.each(data,function(p,pro){
					 var $d ="";
					 var typeName = proTypeName(pro);
					 var $detailTitleStr = '';
					 var proDetailList = pro.proDetailMapList;
					 if(CommnUtil.notNull(typeName) && CommnUtil.notNull(JSON.stringify(proDetailList))){
						 $d +='<p class="d"><span class="red spe_offer"><i>'+typeName+'</i></span>' 
					 }
					 if(CommnUtil.notNull(JSON.stringify(proDetailList))){
						 $detailTitleStr =  proDetail(proDetailList);
						 $d += $detailTitleStr;
						 $d += '</p>';
					 }
					// alert($(".store .third div .b").html());
					/* var $spe_offer =  $(".store .third .right > .b > .d > .spe_offer > i ");
					 if($spe_offer.length > 0 ){//如果促销活动存在
						 $spe_offer.each(function(){
							 alert($(this).text());
						    if($(this).text() == typeName){
								 $spe_offer.parent().parent().append($detailTitleStr);
						    }
						 });
					 }else{
					    $(".store .third .right > .b").append($d); 
					 }*/
					    $(".store .third .right > .b").append($d); 
					  
				});
			}else{
				 var $d ="";
				 $d +='<p class="d"><span class="red spe_offer"><i>暂无优惠</i></span></p>' 
				 $(".store .third .right > .b").append($d); 
				/* var price = $("#marketPrice_id").html();
				 $("#memberMarketPrice").text(price);*/
			}
		}else {	
			layer.msg(result.resultDesc,{icon:1,skin: 'layui-layer-red ',offset: ['40%', '45%'],offset: ['40%', '45%']});
		}
	}
}
function proDetail(proDetailList){
	var $detailTitleStr = '';
	if(CommnUtil.notNull(JSON.stringify(proDetailList))){
		$.each(proDetailList,function(d,proDetail){	
			var $title = proDetail.proDetailTitle;
			$detailTitleStr += $title +",";
		});
	}
	return $detailTitleStr;
}
//促销名称
function proTypeName(pro){
	var proTypeName ="";
	if(CommnUtil.notNull(pro.pro_type)){
		 if('0' == pro.pro_type ){ //订单满减 
			 proTypeName = "订单满减";
		 }else if('1' == pro.pro_type){//商品满减
			 proTypeName =  "商品满减";
		 }else if('2' == pro.pro_type){//商品满赠
			 proTypeName =  "商品满赠";
		 }else if('3' == pro.pro_type){//商品打折
			 proTypeName = "商品打折";
		 }else{  //不参与促销活动
			 proTypeName = "";
		 }
	}
    return proTypeName;
	 
}
/**
 * @title 添加或取消收藏
 * @param selected 商品是否已被收藏（0：否，1：是） 
 */
function addOrCacelFavorites(selected){
	//获取商品Id 
	var id = $("#goodsIdd").val();
	//判断用户是否登录
    if(member == null){
    	window.location.href=rootPath + '/member/loginPre?redirect=/goods/queryGoodsDetail?id='+id;
    	return false;
    }
    var url = rootPath + "/cart/addOrCacelFavorites";
	var data={};
	data["goodsId"] = id;
	data["selected"]=selected;
	data["sellerId"] ="1"; //默认企商云一个店铺
	data["collectionType"] =  "1";  //收藏类型 1：商品，2：店铺 
	var result = CommnUtil.ajax(url,data,"json");
	if(result.resultSuccess){
		if(selected == '0'){
			layer.msg("取消收藏成功!",{icon:1,skin: 'layui-layer-red ',offset: ['40%', '45%']});
		}else if(selected ='1'){
			layer.msg("收藏成功!",{icon:1,skin: 'layui-layer-red ',offset: ['40%', '45%']});
		}
		
	}else if(result.sessionValid){
    	window.location.href=rootPath + '/member/loginPre?redirect=/goods/queryGoodsDetail?id='+id;
    	return false;
	}else  if(result.resultSuccess == false){
		layer.msg(result.resultDesc,{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
    	return false;
	}
}

//立即购买商品	
function goBuy(){
	//获取商品Id
	var id = $("#goodsIdd").val();
	//判断用户是否登录
    if(member == null){
    	window.location.href=rootPath + '/member/loginPre?redirect=/goods/queryGoodsDetail?id='+id;
    	return false;
    }

    if($("#divTest .color > span").length > 0 && $("#divTest .size > span").length > 0 ){ //有颜色和尺码规格
    	 if($("#divTest .color").children(".selected").length == 0 &&  $("#divTest .size").children(".selected").length == 0){ //什么都没选
    		 layer.msg("请选择商品规格!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
    	     return false;
    	 }else if($("#divTest .color").children(".selected").length >= 0 &&  $("#divTest .size").children(".selected").length == 0){//没有选择尺码
    		 layer.msg("请选择商品尺码!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
    		 return false;
    	 }else if($("#divTest .color").children(".selected").length == 0 &&  $("#divTest .size").children(".selected").length > 0){//没有选择规格
    		 layer.msg("请选择商品颜色!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
    		 return false;
    	 }
    }else if($("#divTest .color > span").length > 0 && $("#divTest .size > span").length == 0 ){//单规格只有颜色
    	 if($("#divTest .color").children(".selected").length == 0){
    		 layer.msg("请选择商品颜色!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
    		 return false;
    	 }
    }else if($("#divTest .color > span").length == 0 && $("#divTest .size > span").length > 0 ){//单规格只有尺码
	   	 if($("#divTest .size").children(".selected").length == 0){
			 layer.msg("请选择商品尺码!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
			 return false;
		 }
	}
	if( parseInt($(".store .third div .c div .inventory > span").text()) == 0){ //库存为0
		layer.msg("该规格商品已售完,请重新选择!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%'],offset: ['40%', '45%']});
		return false;
	}
	//遍历获取div的内容
	var colorAndSize="";
	var goodsSkuValueIds = "";
	$("#divTest div").children(".selected").each(function(){
		var cs = $(this).text();
		var goodsSkuValueId = $(this).attr("goodsSkuValueId");
		colorAndSize += cs+",";
		goodsSkuValueIds += goodsSkuValueId +",";
	 }); 
	var url = rootPath + "/order/goBuy";
	var data={};
	data["goodsId"] = id;
	data["goods_sku_id"]=goodsSkuValueIds;
	data["goods_sku_info"] =colorAndSize;
	data["mount"] =$("#divTest .number input").val();
	//alert($("#divTest .number input").val());
	var result = CommnUtil.ajax(url,data,"json");
	//alert("立即购买.........商品ID:"+id+"===商品规格:"+colorAndSize)
	if(result.resultSuccess){
		//alert("加入购物车成功!");
		var $url = rootPath + '/order/product_confirm_order';		
		window.open($url,'_self'); 
	}else if(result.sessionValid){
    	window.location.href=rootPath + '/member/loginPre?redirect=/goods/queryGoodsDetail?id='+id;
    	return false;
	}else if(result.resultSuccess == false){
		layer.msg(result.resultDesc,{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
    	return false;
	}
}	
//加入购物车
function goCart(){
	//获取商品Id
	var id = $("#goodsIdd").val();
	//判断用户是否登录
    if(member == null){
    	window.location.href=rootPath + '/member/loginPre?redirect=/goods/queryGoodsDetail?id='+id;
    	return false;
    }
    if($("#divTest .color > span").length > 0 && $("#divTest .size > span").length > 0 ){
   	 if($("#divTest .color").children(".selected").length == 0 &&  $("#divTest .size").children(".selected").length == 0){
   		 layer.msg("请选择商品规格!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
   	     return false;
   	 }else if($("#divTest .color").children(".selected").length >= 0 &&  $("#divTest .size").children(".selected").length == 0){
   		 layer.msg("请选择商品尺码!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
   		 return false;
   	 }else if($("#divTest .color").children(".selected").length == 0 &&  $("#divTest .size").children(".selected").length > 0){
   		 layer.msg("请选择商品颜色!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
   		 return false;
   	 }
   }else if($("#divTest .color > span").length > 0 && $("#divTest .size > span").length == 0 ){
   	 if($("#divTest .color").children(".selected").length == 0){
   		 layer.msg("请选择商品颜色!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
   		 return false;
   	 }
   }else if($("#divTest .color > span").length == 0 && $("#divTest .size > span").length > 0 ){
	   	 if($("#divTest .size").children(".selected").length == 0){
			 layer.msg("请选择商品颜色!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
			 return false;
		 }
	}
	if( parseInt($(".store .third div .c div .inventory > span").text()) == 0){
		layer.msg("该规格商品已售完,请重新选择!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%'],offset: ['40%', '45%']});
		return false;
	}
	if( parseInt($(".store .third div .c div .inventory > span").text()) == 0){
		layer.msg("该规格商品已售完,请重新选择!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%'],offset: ['40%', '45%']});
		return false;
	}
	//遍历获取div的内容
	var colorAndSize="";
	var goodsSkuValueIds = "";
	$("#divTest div").children(".selected").each(function(){
		var cs = $(this).text();
		var goodsSkuValueId = $(this).attr("goodsSkuValueId");
		colorAndSize += cs+",";
		goodsSkuValueIds += goodsSkuValueId +",";
	 }); 
	var url = rootPath + "/cart/addProduct";
	var data={};
	data["productId"] = id;
	data["goods_sku_id"]=goodsSkuValueIds;
	data["goods_sku_info"] =colorAndSize;
	data["memberId"] = member.mId;
	data["mount"] =$("#divTest .number input").val();
	//alert($("#divTest .number input").val());
	var result = CommnUtil.ajax(url,data,"json");
	//alert("立即购买.........商品ID:"+id+"===商品规格:"+colorAndSize)
	if(result.resultSuccess){
		layer.msg("加入购物车成功!",{icon:1,skin: 'layui-layer-red ',offset: ['40%', '45%']});
	}else if(result.sessionValid){
    	window.location.href=rootPath + '/member/loginPre?redirect=/goods/queryGoodsDetail?id='+id;
    	return false;
	}else if(result.resultSuccess == false){
		layer.msg(result.resultDesc,{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
    	return false;
	}
}